﻿
Imports std
Imports managedServer

Public Module lpmsserv

    Public Const defaultConfigfile As String = "lpmsserv.ini"
    Private scriptpath As String = Nothing
    Private locpath As String = Nothing
    Public shareapp As lpmsmonitor = Nothing
    Public globalInitial As String = Nothing
    Public config As configParser = Nothing

    Public Class processorConfig
        Public scriptfile As String
        Public path As String
        Public cache As Int64
    End Class

    'script0 = scriptfile,path,cache 
    Private Function parseScriptline(ByVal s As String, ByRef pc As processorConfig) As Boolean
        Dim words As vector(Of String) = Nothing
        strsepar(s, character.comma, words)
        If words Is Nothing OrElse words.size() < 1 Then
            Return False
        Else
            pc = New processorConfig()
            pc.scriptfile = combinepath(scriptpath, words(0))
            pc.path = ifoper(words.size() = 1, network.pathSeperator + words(0), words(1))
            kickLeftRightBlank(pc.path)
            If words.size() < 2 OrElse Not convertor.toint64(words(2), pc.cache) Then
                pc.cache = 0
            End If

            Return True
        End If
    End Function

    Public Sub main(ByVal args() As String)
        debugpause()
        AddHandler ProHttpS.configChanged, AddressOf initial
        AddHandler ProHttpS.initial, AddressOf initial
        ProHttpS.defaultConfigfile = defaultConfigfile
        'seems i should try to use storoomTalks here
        ProHttpS.startServer(args, contextParameter:=New httpContext.parameterDefinition(stringsPackage:=sp))
    End Sub

    Private Function createRequestProcessor(ByVal pc As processorConfig) As Global.requestProcessor
        assert(Not pc Is Nothing, "processorConfig is nothing.")
        If pc.cache > 0 Then
            Return New cacheProcessor(pc.path, pc.cache, pc.scriptfile)
        Else
            Return New requestProcessor(pc.path, pc.scriptfile)
        End If
    End Function

    Private Function createRequestProcessor(ByVal script As String, ByRef rp As Global.requestProcessor, _
                                            Optional ByVal defaultscript As Boolean = False) As Boolean
        Dim pc As processorConfig = Nothing
        If parseScriptline(script, pc) Then
            If defaultscript Then
                pc.path = network.pathSeperator
            End If
            rp = createRequestProcessor(pc)
            If Not rp Is Nothing Then
                Return True
            Else
                raiseError("cannot create requestProcessor for script " + script + ", ignore.", _
                           errorHandle.errorType.warning)
                Return False
            End If
        Else
            raiseError("cannot parse script line " + script + ", ignore.", errorHandle.errorType.warning)
            Return False
        End If
    End Function

    Private Sub insertScriptRequestProcessor(ByVal script As String, Optional ByVal defaultscript As Boolean = False)
        Dim rp As Global.requestProcessor = Nothing
        If createRequestProcessor(script, rp, defaultscript) Then
            assert(Not rp Is Nothing, "rp is nothing.")
            ProHttpS.insertRequestProcessor(rp)
        End If
    End Sub

    Private Sub initial(ByVal config As configMonitor)
        lpmsserv.config = config
        scriptpath = config.parameter(configNaming.lpmsserv.name, configNaming.lpmsserv.scriptpath, _
                                      configNaming.defaultValue.lpmsserv.scriptpath)

        locpath = config.parameter(configNaming.lpmsserv.name, configNaming.lpmsserv.locpath, _
                                   configNaming.defaultValue.lpmsserv.locpath)
        initialStringsPackage(locpath)

        Dim sharescript As String = Nothing
        sharescript = config.parameter(configNaming.lpmsserv.name, configNaming.lpmsserv.share, emptyString)
        If Not isemptystring(sharescript) Then
            shareapp = New lpmsmonitor(combinepath(scriptpath, sharescript))
            globalInitial = config.parameter(configNaming.lpmsserv.name, configNaming.lpmsserv.globalInitial, _
                                             configNaming.defaultValue.lpmsserv.globalInitial)
            lpmsmonitor.invokeglobalinitial()
        End If

        ProHttpS.managedRequestProcessors().clear()
        ProHttpS.insertRequestProcessor(New storoomDelegate())
        Dim scripts As vector(Of String) = Nothing
        If config.readlist(configNaming.lpmsserv.name, configNaming.lpmsserv.script, scripts) AndAlso _
           Not scripts Is Nothing AndAlso scripts.size() > 0 Then
            Dim i As Int64
            For i = 0 To scripts.size() - 1
                insertScriptRequestProcessor(scripts(i))
            Next
        Else
            raiseError("cannot read script list from configuration.", errorHandle.errorType.warning)
        End If

        Dim defaultscript As String = Nothing
        If config.getparameter(configNaming.lpmsserv.name, configNaming.lpmsserv.default, defaultscript) Then
            insertScriptRequestProcessor(defaultscript, True)
        End If
    End Sub
End Module

Namespace configNaming
    Namespace lpmsserv
        Public Module lpmsserv
            Public Const name As String = "lpmsserv"
            Public Const scriptpath As String = "scriptpath"
            Public Const locpath As String = "locpath"
            Public Const script As String = "script"
            Public Const share As String = "share"
            Public Const globalInitial As String = "globalInitial"
            Public Const [default] As String = "default"
        End Module
    End Namespace

    Namespace storoom
        Public Module storoom
            Public Const name As String = "storoom"
            Public Const storoomhost As String = "storoomhost"
            Public Const storoomport As String = "storoomport"
            Public Const storoomretrytimes As String = "storoomretrytimes"
        End Module
    End Namespace

    Namespace loc
        Public Module loc
            Public Const name As String = "loc"
            Public Const language As String = "language"
        End Module
    End Namespace

    Namespace defaultValue
        Namespace lpmsserv
            Public Module lpmsserv
                Public Const scriptpath As String = "script"
                Public Const locpath As String = "loc"
                Public Const globalInitial As String = "global_initial"
            End Module
        End Namespace

        Namespace storoom
            Public Module storoom
                Public Const storoomhost As String = "hzj-jie-8320-1"
                Public Const storoomport As Int32 = 42412
                Public Const storoomretrytimes As Int64 = storoomc.common.talkTime.client2room.retrytimes
            End Module
        End Namespace

        Namespace loc
            Public Module loc
                Public Const language As String = stringsPackage.defaultLanguage
            End Module
        End Namespace
    End Namespace
End Namespace

Public Module lpmsservshare
    Public Function currentContext() As httpContext
        Return httpServer2.currentContext()
    End Function
End Module